

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

//import org.apache.log4j.Logger;

import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;


public class jackaccess {

	
	//private  String fileMDB = null;
	//private static String fileMDB = "D:/personal/sugar_work/CSIDB.mdb";
	//private static String fileMDB = "";
	private static String mainTable = "tabAllInformations";
	
	//private static Logger log = Logger.getLogger(jackaccess.class);  
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		String serverLU = args[0]; 
		String fileMDB = args[1]; 
		
		//System.out.println(serverLU);
		//System.out.println(fileMDB);
		
		Database db = Database.open(new File(fileMDB));
		
		Table table = db.getTable(mainTable);
		
		//System.out.println(Database.open(new File("D:/personal/sugar_work/CSIDB.mdb")).getTable("tabAllInformations").getRowCount());
		
		String it_responsible = null;
		String system_owner = null;
		
		Map<String, Object> row = Cursor.findRow(table, Collections.singletonMap("Server_ServerName", serverLU));
		if(row != null) {
		  String App = (String) row.get("Application");
		  if (row.get("it_responsible_id") != null) {
			  int it_owner =  (Integer) row.get("it_responsible_id");
			  it_responsible = lookupTable(it_owner,fileMDB);
		  }
		  
		  if (row.get("system_owner_id") != null) {
			int system_owner_id = (Integer) row.get("system_owner_id");
			system_owner = lookupTable(system_owner_id,fileMDB);
		  //}
		 // System.out.println(system_owner);
		} 
	
		  System.out.println(App);
		  System.out.println(it_responsible);
		
		System.out.println(system_owner);
		  
		db.close();
		}
	}
	
	public static String lookupTable (int lu, String fileMDB) throws IOException{
		
		Database db = Database.open(new File(fileMDB));
		Table table = db.getTable("tbl_ddv");
		Map<String, Object> row = Cursor.findRow(table, Collections.singletonMap("ddv_id", lu));
		if(row != null) {
			return (row.get("ddv_value").toString());
		  
		 // System.out.println(system_owner);
		} else {
			return "Not found in table " + lu;
		}
	}
}
